//
//  CanvasView.m
//  TouchPainter
//
//  Created by Carlo Chung on 9/14/10.
//  Copyright 2010 Carlo Chung. All rights reserved.
//

#import "CanvasView.h"
#import "MarkRenderer.h"
#import "UIImage+Resize.h"
#import <QuartzCore/QuartzCore.h>

@implementation CanvasView

@synthesize mark=mark_, backgroundImage=_backgroundImage, drawableRectangle =_drawableRectangle,picture =_picture;


- (id)initWithFrame:(CGRect)frame 
{
  if ((self = [super initWithFrame:frame])) 
  {
    // Initialization code
    [self setBackgroundColor:[UIColor blackColor]];
      _drawableRectangle = self.frame;
      _originFrame = self.frame;
    if(nil != self.backgroundImage)
{
    [self drawBackgroundWithImage:self.backgroundImage];
}

  }
  return self;
}
-(void) drawBackgroundWithImage:(UIImage*) image
{
  //  UIColor *background = [[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"background_texture.png"]]autorelease];

//    if( nil != image)
//    {
//        CGSize newSize = CGSizeZero;
//        if (image.size.width< image.size.height) {
//            newSize.height = self.frame.size.height;
//            newSize.width = ((float)(image.size.width/image.size.height))*newSize.height;
//        }
//        else
//        {
//            newSize.width = self.frame.size.width;
//            newSize.height = ((float)(image.size.height/image.size.width))*newSize.width;
//        }
//        UIImage *resizeImage = [image resizedImage:newSize interpolationQuality:kCGInterpolationHigh];
//    UIColor *background = [[[UIColor alloc] initWithPatternImage:resizeImage]autorelease];
//    self.backgroundColor = background;
//    }
//    else if(nil != self.backgroundImage)
//    {
//        UIColor *background = [[[UIColor alloc] initWithPatternImage:self.backgroundImage]autorelease];
//        self.backgroundColor = background;
//    }
//    else [self setBackgroundColor:[UIColor whiteColor]];

    
}
-(void) setBackgroundImage:(UIImage *)backgroundImage
{
    if (nil!=backgroundImage) {
        UIImage *image = backgroundImage;
        CGSize newSize = CGSizeZero;
        if (image.size.width< image.size.height) {
            newSize.height = _originFrame.size.height;
            newSize.width = ((float)(image.size.width/image.size.height))*newSize.height;
        }
        else
        {
            newSize.width = _originFrame.size.width;
            newSize.height = ((float)(image.size.height/image.size.width))*newSize.width;
        }
        UIImage *resizeImage = [image resizedImage:newSize interpolationQuality:kCGInterpolationHigh];
        
        _backgroundImage = [resizeImage retain];
        //_drawableRectangle = CGRectMake(CGRectGetMidX(self.frame)-newSize.width/2, CGRectGetMidY(self.frame)-newSize.height/2, newSize.width, newSize.height);
        self.frame = CGRectMake(CGRectGetMidX(_originFrame)-newSize.width/2, CGRectGetMidY(_originFrame)-newSize.height/2, newSize.width, newSize.height);
    }
    
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect 
{
    //UIGraphicsBeginImageContext(rect.size);
    //[self.layer renderInContext:UIGraphicsGetCurrentContext()];
    [self.backgroundImage drawInRect:rect];
    
  // Drawing code
  CGContextRef context = UIGraphicsGetCurrentContext();
  
  // create a renderer visitor
  MarkRenderer *markRenderer = [[[MarkRenderer alloc] initWithCGContext:context] autorelease];
  
  // pass this renderer along the mark composite structure
  [mark_ acceptMarkVisitor:markRenderer];
  // _picture = UIGraphicsGetImageFromCurrentImageContext();
  // UIGraphicsEndImageContext();
  // [_picture drawInRect:rect];
}
-(UIImage*) picture
{
    if(UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
    } else {
        UIGraphicsBeginImageContext(self.frame.size);
    }
    
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();  
    return image;
}

- (void)dealloc 
{
  [mark_ release];
  [super dealloc];
}

@end
